package com.qiangqiang.resoldhouse.controller;

import com.qiangqiang.resoldhouse.entity.Agent;
import com.qiangqiang.resoldhouse.entity.User;
import com.qiangqiang.resoldhouse.service.HouseResourceService;
import com.qiangqiang.resoldhouse.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author : sky
 * @date : 2018/8/13
 * @description : 用户 controller
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private HouseResourceService houseService;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(User user) {
        //判断是否用户名是否重复
        boolean checkAccount = userService.findUserByAccount(user.getAccount()).size() > 0;
        if (checkAccount) {
            return "error_register";
        }
        userService.addUser(user);
        return "login_user";
    }

    @RequestMapping(value = "/user_login", method = RequestMethod.POST)
    public String userLogin(User user, HttpSession session) {
        
        if (!userService.validateUserPassword(user)) {
            return "error_login";
        }
        // 这里增加操作，session、绑定用户名等
        user = userService.findUserByAccount(user.getAccount()).get(0);
        //经纪人和用户不能同时登录
        session.removeAttribute("agent");
        session.setAttribute("user", user);
        return "redirect:/index";
    }

    //用户或者经纪人进入个人中心
    @RequestMapping("/center")
    public String getIntoCenter(Map<String, Object> map, HttpSession session){
        if(session.getAttribute("agent") != null){
            Agent agent = (Agent)session.getAttribute("agent");
            map.put("agent", agent);
            return "agentPage/index";
        }
        else if(session.getAttribute("user") != null){
            User user = (User) session.getAttribute("user");
            map.put("user", user);
            return "userPage/index";
        }
        //如果都获取不到说明是未登录状态，重定向到登陆页面
        return "redirect:/login_user";
    }

    @RequestMapping("/user_appointment_list")
    public String getSecond(Map<String, Object> map, HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user == null){
            return "redirect:/login_user";
        }
        map.put("user", user);
        return "userPage/third";
    }

    @RequestMapping("/update_user_before")
    public String updateUserBefore( Map<String, Object> map, HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user == null){
            //说明是未登录状态，跳转到登录界面
            return "redirect:/login_user";
        }
        map.put("user", user);
        return "userPage/sixth";
    }

    @RequestMapping("/update_user_after")
    public String updateUserAfter(HttpSession session, User user){
        if(session.getAttribute("user") == null){
            return "redirect:/login_user";
        }
        Boolean result = userService.updateUser(user);

        //更新后，放在session的user的信息也需要更新
        session.setAttribute("user", user);

        return "redirect:/user/center";
    }

}
